//
//  HEStoreGoodListViewController.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/6/10.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEStoreGoodListViewController.h"
#import "HEGoodsCell.h"
#import "HEGoodsDetailsViewController.h"

@interface HEStoreGoodListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEGoodsModel *> * dataSourceArr;

@end

@implementation HEStoreGoodListViewController

- (NSMutableArray<HEGoodsModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"商品列表";
    self.view.backgroundColor = KWhiteColor;
    
    _pageNum = 1;
    _pageSize = 20;
    
    [self initUI];
    [self loadData];
    
    [self setCustomTableView:_tableView];
}

- (void)initUI {
  
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - ZXSafeAreaBottom) style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");
    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];


    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];

    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = NO;
        weakSelf.pageNum += 1;
        [weakSelf loadData];
    }];

    [_tableView registerClass:[HEGoodsCell class] forCellReuseIdentifier: KCellGoodId];
}

 
#pragma mark-

- (void)loadData {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[NSNumber numberWithInteger:_pageNum] forKey:@"pageNum"];
    [dict setValue:[NSNumber numberWithInteger:_pageSize] forKey:@"pageSize"];
    [dict setValue:[NSNumber numberWithInteger:_storeId] forKey:@"storeId"];

    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KGoodsList parameters:dict success:^(id  _Nonnull responseObject) {
 
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEMallPageResponse *response = [HEMallPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEGoodsModel mj_objectArrayWithKeyValuesArray:response.data.records]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.records.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier: KCellGoodId];
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KCellGoodId cacheByIndexPath:indexPath configuration:^(HEGoodsCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEGoodsModel *model = _dataSourceArr[indexPath.row];
    HEGoodsDetailsViewController *vc = [HEGoodsDetailsViewController new];
    vc.goodsId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
